package com.quick.calcu.controller;

import com.quick.calcu.entity.WrongQuestion;
import com.quick.calcu.service.StudentService;
import com.quick.calcu.service.WrongQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/wrong-questions")
public class WrongQuestionController {

    @Autowired
    private WrongQuestionService wrongQuestionService;

    @Autowired
    private StudentService studentService;
    // 根据ID查询错题
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getWrongQuestion(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            WrongQuestion question = wrongQuestionService.getWithDetails(id);
            if (question != null) {
                result.put("code", "200");
                result.put("message", "查询成功");
                result.put("data", question);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "错题不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 根据学生ID查询错题列表
    @GetMapping("/student/{studentId}")
    public ResponseEntity<Map<String, Object>> getByStudentId(@PathVariable Long studentId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<WrongQuestion> questions = wrongQuestionService.getByStudentId(studentId);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", questions);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 添加错题
    @PostMapping
    public ResponseEntity<Map<String, Object>> addWrongQuestion(@RequestBody WrongQuestion question) {
        Map<String, Object> result = new HashMap<>();
        try {
            long s = studentService.getByUserId(question.getStudentId()).getStudentId();
            question.setStudentId(s);
            boolean success = wrongQuestionService.save(question);
            result.put("code", "200");
            result.put("data", question);
            result.put("message", success ? "添加成功" : "添加失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }


    //根据学生id删除错题,删除当前周上周的错题
    @DeleteMapping("/deleteByStudentId/{studentId}")
    public ResponseEntity<Map<String, Object>> deleteByStudentIdAndTime(@PathVariable Long studentId){
        Map<String, Object> result = new HashMap<>();
        try {
            // 计算上周的开始和结束日期
            LocalDate today = LocalDate.now();
            LocalDate startOfLastWeek = today.minusWeeks(1).with(java.time.DayOfWeek.MONDAY);
            LocalDate endOfLastWeek = today.minusWeeks(1).with(java.time.DayOfWeek.SUNDAY);

            boolean success = wrongQuestionService.deleteByStudentIdAndTime(studentId, startOfLastWeek, endOfLastWeek);
            result.put("code", "200");
            result.put("message", success ? "删除成功" : "删除失败");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

}